package pt.ipl.isel.deetc.leic.pdm.g12.yamba;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class TimelineActivity extends BaseActivity implements OnItemClickListener  {
	private ListView listTimeline;
	private SimpleAdapter adapter = null;
	private final String[] from = {Status.KEY_USER, Status.KEY_TIME, Status.KEY_STATUS_PREVIEW};
	private final int[] to = {R.id.statusTime, R.id.statusUser, R.id.statusText};
	private Timer autoUpdate;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.timeline);

		listTimeline = (ListView)findViewById(R.id.listTimeline);
		listTimeline.setOnItemClickListener(this);
		
		updateTimeline();
		
		Log.d(YambaPdmApp.TAG, "onCreate Timeline");
	}
	
	@Override
	protected void onResume() {		
		super.onResume();
		Boolean timelineUpdateIntervalCheck = yamba.getPreferences().getBoolean("timelineUpdateIntervalCheck", true);
		
		if(timelineUpdateIntervalCheck) {
			String timelineUpdateInterval = yamba.getPreferences().getString("timelineUpdateInterval", "5000");
			autoUpdate = new Timer();
			autoUpdate.schedule(new TimerTask() {
				@Override
				public void run() {
					runOnUiThread(new Runnable() {
						public void run() {
							updateTimeline();
							Log.d(YambaPdmApp.TAG, "autoUpdate");
						}
					});
				}
			}, 0, Integer.parseInt(timelineUpdateInterval) * 1000);
		}
		else
			updateTimeline();
	}
	
	@Override
	public void onPause() {
		autoUpdate.cancel();
		super.onPause();
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.removeItem(R.id.timeline);
		
		return super.onPrepareOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {		
		switch(item.getItemId()){
		case R.id.refreshTimeline:			
			updateTimeline();			
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	protected void updateTimeline() {
		Boolean timelineUpdateIntervalCheck = yamba.getPreferences().getBoolean("timelineUpdateIntervalCheck", true);
		if(!timelineUpdateIntervalCheck || yamba.listStatus == null){
			yamba.updateStatusList();
		}			

		if(yamba.listStatus != null) {
			adapter = new SimpleAdapter(this, yamba.listStatus, R.layout.timeline_row, from, to);		
			listTimeline.setAdapter(adapter);
		}
	}
	
	public void onItemClick(AdapterView<?> a, View view, int pos, long id) {
		Intent intent = new Intent(this, DetailActivity.class);
		Status status = yamba.listStatus.get(pos);
		
		intent.putExtra("id", status.getId());
		intent.putExtra("user", status.getUser());
		intent.putExtra("time", status.getTime());
		intent.putExtra("status", status.getStatus());
		
		startActivity(intent);
	}
}
